"""
$Id: ui.py 2 2007-03-04 19:08:44Z sidnei.da.silva $
"""

import random
import logging
import threading
from voting import constants
from Tkinter import *

class UI(object):

    def __init__(self, zone_id, sections, valid):
        self.logger = logging.getLogger('voting.ui')
        self.uis = []
        for section in sections:
            self.uis.append(_RealUI(zone_id, section, valid))

    def start(self):
        for ui in self.uis:
            ui.start()

    def stop(self):
        for ui in self.uis:
            ui.stop()

    def join(self):
        for ui in self.uis:
            ui.join()

class _RealUI(threading.Thread):

    def __init__(self, zone_id, section, valid):
        threading.Thread.__init__(self)
        self.logger = logging.getLogger('voting.ui')
        self.zone_id = zone_id
        self.section_id, self.section = section
        self.valid = list(valid)
        self.shutdown = False

    def setup(self):
        self.master = Tk()
        self.master.title('Zone %s - Section %s' % (self.zone_id,
                                                    self.section_id))

        self.frame = Frame(self.master)
        self.frame.pack()

        self.label = Label(self.frame, text='Elector ID')
        self.label.pack(side=LEFT)

        self.elector = Entry(self.frame)
        self.elector.pack(side=LEFT)

        self.vote = Entry(self.frame)
        self.vote.pack(side=LEFT)

        self.button = Button(self.frame, text="Vote",
                             command=self.do_vote)
        self.button.pack(side=RIGHT)

        self.quit = Button(self.frame, text="Quit",
                           command=self.do_quit)
        self.quit.pack(side=RIGHT)

    def do_quit(self):
        self.shutdown = True

    def stop(self):
        self.do_quit()

    def do_vote(self):
        elector = self.elector.get()
        if not elector in self.valid:
            self.elector.config(background='red')
            return False
        else:
            self.elector.config(background='white')

        try:
            vote = int(self.vote.get())
        except ValueError:
            self.vote.config(background='red')
            return False
        else:
            if not vote in constants.VALID_CHOICES:
                self.vote.config(background='red')
                return False
            else:
                self.vote.config(background='white')

        self.logger.debug('Choosen section %s for '
                          'voting with elector %s',
                          self.section_id, elector)
        self.section.vote(elector, vote)

    def run(self):
        self.setup()
        while True:
            if self.shutdown:
                break
            self.master.update_idletasks()
            if self.shutdown:
                break
            self.master.update()
        self.master.destroy()

if __name__ == '__main__':
    class Section:
        def vote(self, elector, vote):
            print elector, vote

    one = UI([(0, Section())], ['1'])
    one.start()

    two = UI([(0, Section())], ['1'])
    two.start()

    one.join()
    two.join()
